package com.tedu.egweibo1.exception;

import com.tedu.egweibo1.result.JsonResult;
import com.tedu.egweibo1.result.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException e) {
        log.error(e.getMessage(), e);

        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String message=e.getFieldError().getDefaultMessage();
        log.error(message, e);
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }
}
